package org.argus.job.server.gateway.core.config;

import org.argus.job.server.common.constants.JobCacheConstants;
import org.argus.job.server.gateway.core.cache.AppAssignCacheMaintainer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * App绑定/分配的 Job Server事件监听配置
 * 
 * @Author 刘文/Cliff Liu
 * @Date 2024/11/23
 */
public class AppAssignRedisListenerConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
        AppAssignCacheMaintainer appAssignCacheMaintainer) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener((message, pattern) -> appAssignCacheMaintainer.loadAssignResultFromRedis(),
            new PatternTopic(JobCacheConstants.APP_ASSIGNED_TOPIC));
        return container;
    }
}
